export const Mixin = {

  data() {
    return {
      disableMixinCreated: false,
      loading: false, // 遮罩层
      ids: [], // 选中数组
      single: true, // 非单个禁用
      multiple: true, // 非多个禁用
      showSearch: true, // 显示搜索条件
      total: 0, // 总条数
      dataSource: [], // 列表数据
      queryParams: {}, // 查询条件
      title: '', // 弹出层标题
      open: false, // 是否显示弹出层
      form: {}, // 表单参数
      rules: {}, // 表单校验
      url: {
        list: '',
        deletes: '',
        add: '',
        edit: ''
      },
      // tableHeight: null
    }
  },
  created() {
    if (!this.disableMixinCreated) {
      this.loadData()
    }
  },
  // mounted() {
  //   this.getTableHeight()
  //   window.addEventListener('resize', this.getTableHeight)
  // },
  methods: {
    // getTableHeight() {
    //   this.tableHeight = window.innerHeight - this.$refs.table.$el.offsetTop - 110
    // },
    /** 查询字典列表 */
    loadData() {
      if (!this.url.list) {
        this.msgInfo('请设置url.list属性!')
        return
      }
      this.loading = true
      this.$http.get(this.url.list, this.queryParams).then(response => {
        this.dataSource = response.data
        this.total = response.data.length
        this.loading = false
      })
    },
    /** 查询按钮操作 */
    handleQuery() {
      this.queryParams.page = 1
      this.loadData()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.queryForm.resetFields()
      this.queryParams = {}
      this.handleQuery()
    },
    /** 弹出框取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    },
    /** 表单重置 */
    reset() {
      this.form = {}
      this.resetForm('form')
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '新增'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      if (row.type && row.currentTarget) {
        this.form = this.dataSource.find(item => item.id === this.ids[0])
      } else {
        this.form = Object.assign({}, row)
      }
      this.open = true
      this.title = '修改'
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return this.$http.post(this.url.deletes + '?entityIds=' + ids)
      }).then(() => {
        this.loadData()
        this.msgSuccess('删除成功')
      }).catch(function() {
      })
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            this.$http.post(this.url.edit, this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('修改成功')
                this.open = false
                this.loadData()
              }
            })
          } else {
            this.$http.post(this.url.add, this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('新增成功')
                this.open = false
                this.loadData()
              }
            })
          }
        }
      })
    },
    /** 导出按钮操作 */
    handleExport(fileName = '数据列表') {
      const params = this.removePageParams(this.queryParams)
      const tHeader = this.columns.map(v => v.label)
      const filterVal = this.columns.map(v => v.prop)
      this.$http.get(this.url.list, params).then(response => {
        this.export(response.rows, tHeader, filterVal, fileName)
      })
    }
  }
}
